/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rnavizexperimentation;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Arc2D;
import java.util.LinkedList;
import javax.swing.JApplet;
import javax.swing.JFrame;

/**
 *
 * @author Elias la Cour
 */
public class DrawShapes extends JApplet {

    static char[] sequence;
    static char[] knots;
    final static BasicStroke stroke = new BasicStroke(1.0f);
    LinkedList<Knot> edges;

    @Override
    public void init() {
        setBackground(Color.white);
        setForeground(Color.white);
        tieKnots();
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.black);
        g2.setStroke(stroke);
        for (int i = 0; i < knots.length; i++) {
            int x = 10 * i;
            drawElement(sequence[i], x, 250, g2);

        }
        while (!edges.isEmpty()) {
            Knot e = edges.pop();
            int b1X= e.getB1().getX();
            int e2X= e.getB2().getX();
            drawArc(b1X,(e2X-b1X),100,g2);
        
        }
    }
/**
     * Used to find edges (=knots). Atm. Creates 1 linkedlist for pseudoknots and 1 for
     * normal knots. Which are then merged
     */
    public void tieKnots() {
        edges = new LinkedList<Knot>();
        LinkedList<Knot> normalKnots = new LinkedList<Knot>();
        LinkedList<Knot> pseudoKnots = new LinkedList<Knot>();

        for (int i = 0; i < knots.length; i++) {
            int x = 10 * i;
            if (knots[i]=='(') {
                Base n = new Base(x, 0);
                normalKnots.add(new Knot(new Base(x, 0)));
                //drawArc(x, 100, 100, g2);
            }
            if (knots[i]=='[') {
                Base n = new Base(x, 0);
                pseudoKnots.add(new Knot(new Base(x, 0)));
            }
            if (knots[i]==')') {
                Base n = new Base(x, 0);
                Knot e = normalKnots.pop();
                e.setB2(n);
                edges.add(e);
            }
            if (knots[i]==']') {
                Base n = new Base(x, 0);
                Knot e = pseudoKnots.pop();
                e.setB2(n);
                edges.add(e);
            }
        }
    }
/**
     * The drawArc method draws a single arc to be used in linear Feyman
     * @param x
     * @param width
     * @param height
     * @param g2 
     */
    private void drawArc(int x, int width, int height, Graphics2D g2) {
        int y = 180;
        g2.draw(new Arc2D.Double(x, y, width, height, 0, 180, Arc2D.OPEN));
    }
/**
     * Draws a single character
     * @param element
     * @param x
     * @param y
     * @param g2 
     */
    private void drawElement(char element, int x, int y, Graphics2D g2) {
        String str = Character.toString(element);
        g2.drawString(str, x, y);
    }

    public static void main(String s[]) {
        JFrame f = new JFrame("");
        InputHandler i = new InputHandler();
        Structure si = i.smallInput();
        sequence = si.getBaseSequence();
        knots = si.getDotSequence();
        f.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        JApplet applet = new DrawShapes();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(1200, 300));
        f.setVisible(true);
    }

    private void drawLogic() {
    }
}
